package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.dao.MarktingCatalogDAO;
import com.ecoolex.ying.space.entity.*;
import com.ecoolex.ying.space.entity.MarktingCatalog.Column;
import com.ecoolex.ying.space.pojo.QueryMarktingCatalogDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;

@Service
public class MarktingCatalogManager {
    @Autowired
    private MarktingCatalogDAO dao;
    @Autowired
    private MarktingCatalogGoodsManager marktingCatalogGoodsManager;

    public MarktingCatalog add(MarktingCatalog record, String createUser) {
        BizAssert.isTrue(Check.notNullOrTrimEmpty(record.getName()), ResultCode.VERIFY.build(10), "名称不能为空");
        record.setOnline(false);
        record.setDeleted(MarktingCatalog.NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
        return record;
    }

    public MarktingCatalog update(MarktingCatalog record, String updateUser) {
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record,
                MarktingCatalog.Column.excludes(MarktingCatalog.Column.online,MarktingCatalog.Column.deleted, MarktingCatalog.Column.createUser, MarktingCatalog.Column.createTime));
        return record;
    }

    public void delete(MarktingCatalog record, String updateUser) {
//        long childCount = marktingCatalogGoodsManager.countCatalogGoodsByCatalogId(record.getCatalogId());
//        BizAssert.isTrue(childCount < 1, ResultCode.BIZ.build(1), "存在{}个关联商品,无法删除", childCount);

        record.setDeleted(MarktingCatalog.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, MarktingCatalog.Column.deleted, MarktingCatalog.Column.updateUser, MarktingCatalog.Column.updateTime);
    }
    
    public void online(MarktingCatalog record, String updateUser) {
    	record.setOnline(true);
    	record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, Column.online,Column.updateUser,Column.updateTime);
    }
    
    public void offline(MarktingCatalog record, String updateUser) {
    	record.setOnline(false);
    	record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, Column.online,Column.updateUser,Column.updateTime);
    }

    public MarktingCatalog getById(Integer catalogId) {
        return dao.selectByPrimaryKeyWithLogicalDelete(catalogId, MarktingCatalog.NOT_DELETED);
    }

    public Pagination<MarktingCatalog> query(QueryMarktingCatalogDTO query, Sorting[] sorts, Page page) {
        MarktingCatalogExample example = new MarktingCatalogExample();
        MarktingCatalogExample.Criteria criteria = example.createCriteria();

        if (Check.notNullOrTrimEmpty(query.getName())) {
            criteria.andNameLike('%' + query.getName()
                    + '%');
        }

        criteria.andDeletedEqualTo(MarktingCatalog.NOT_DELETED);

        example.orderBy(Sortings.builder(sorts).verify(MarktingCatalog.Column::valueOf).toSql());
        example.setOffset(page.getFirstIndex());
        example.setRows(page.getPageSize());

        return Pagination.create(page, dao.countByExample(example), () -> dao.selectByExample(example));
    }

    public Pagination<MarktingCatalog> queryForH5(Page page) {
        MarktingCatalogExample example = new MarktingCatalogExample();
        MarktingCatalogExample.Criteria criteria = example.createCriteria();

        criteria.andDeletedEqualTo(MarktingCatalog.NOT_DELETED).andOnlineEqualTo(true);

        example.orderBy(Column.weight.desc());
        example.setOffset(page.getFirstIndex());
        example.setRows(page.getPageSize());

        return Pagination.create(page, dao.countByExample(example), () -> dao.selectByExample(example));
    }
}